/*
 * Created on 14 mars 2004
 *
 */
package jus.aor.talk.ui.client;

import java.awt.event.ActionEvent;
import javax.swing.*;
import java.util.*;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;

/**
 * @author morat
 *
 */
public class TalkUi extends JFrame implements TalkEventGenerator, IntervenantEventListener {
	private static final long serialVersionUID = -4872455387791136783L;
	protected List<TalkEventListener> talkEventListener = new LinkedList<TalkEventListener>();  //  @jve:decl-index=0:
	/**ajout d'un abonné à la liste*/
	public void addTalkEventListener(TalkEventListener e) {talkEventListener.add(e);}
	/**suppression d'un abonnéde la liste*/
	public void removeTalkEventListener(TalkEventListener e){talkEventListener.remove(e);}
	/** réception de la notification d'évènements ActionEvent*/
	protected void enter(ActionEvent e){
		if(JOptionPane.showInternalOptionDialog(this.getContentPane(),getEnterPanel(),"Enter",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null,null,null)==JOptionPane.CANCEL_OPTION) return;
		if(getForumTextField().getText()==null) return;
		if(getNomTextField().getText()==null) return;
		if(getPrenomTextField().getText()==null) return;
		setTitle(getNomTextField().getText()+" "+getPrenomTextField().getText());
		TalkEventEnter t = new TalkEventEnter(this,getNomTextField().getText(),getPrenomTextField().getText(),getForumTextField().getText());
		for(TalkEventListener x : talkEventListener) x.enter(t);
	}
	protected void say(ActionEvent e){
		int choice = JOptionPane.showOptionDialog(this.getContentPane(),getSayPanel(),"Say dialog",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null,null,null);
		if(choice!=JOptionPane.YES_OPTION) return;
		TalkEventSay t = new TalkEventSay(this,sayWriterArea.getText());
		for(TalkEventListener x : talkEventListener) x.say(t);
	}
	protected void leave(ActionEvent e){
		TalkEventLeave t = new TalkEventLeave(this);
		for(TalkEventListener x : talkEventListener) x.leave(t);
		setTitle("");
	}
	protected void who(ActionEvent e){
		TalkEventWho t = new TalkEventWho(this);
		for(TalkEventListener x : talkEventListener) x.who(t);
	}
	protected void quit(ActionEvent e){
		TalkEventQuit t = new TalkEventQuit(this);
		for(TalkEventListener x : talkEventListener) x.quit(t);
	}
	/**
	* réception de la notification d'un évènement IntervenantEventListen
	*/
	public void listen(IntervenantEventListen e) {
		try{
			writerArea.out.append(e.getMessage());
			writerArea.out.flush();
		}catch(Exception x){
		}
	}
	/**
	* réception de la notification d'un évènement IntervenantEventState1
	*/
	public void gotoState1(IntervenantEventState1 e) {
		enterButton.setEnabled(true);
		leaveButton.setEnabled(false);
		quitButton.setEnabled(true);
		sayButton.setEnabled(false);
		whoButton.setEnabled(false);
	}
	/**
	* réception de la notification d'un évènement IntervenantEventState2
	*/
	public void gotoState2(IntervenantEventState2 e) {
		enterButton.setEnabled(false);
		leaveButton.setEnabled(true);
		quitButton.setEnabled(false);
		sayButton.setEnabled(true);
		whoButton.setEnabled(true);
	}
	/**
	* réception de la notification d'un évènement IntervenantEventWarning
	*/
	public void warning(IntervenantEventWarning e) {
		JOptionPane.showMessageDialog(null,e.getWarning());
	}	
	
	private javax.swing.JPanel jContentPane = null;
	private javax.swing.JPanel jPanel = null;
	private jus.util.WriterArea writerArea = null;
	private javax.swing.JScrollPane jScrollPane = null;
	protected javax.swing.JPanel jPanel1 = null;
	private javax.swing.JButton sayButton = null;
	private javax.swing.JButton clearButton = null;
	private javax.swing.JButton whoButton = null;
	private javax.swing.JButton helpButton = null;
	private javax.swing.JButton enterButton = null;
	private javax.swing.JButton leaveButton = null;
	private javax.swing.JButton quitButton = null;
	private jus.util.WriterArea sayWriterArea = null;
	private javax.swing.JPanel sayPanel = null;
	private javax.swing.JScrollPane jScrollPane1 = null;
	private JPanel enterPanel = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JTextField nomTextField = null;
	private JTextField prenomTextField = null;
	private JTextField forumTextField = null;
	/**
	 * This is the default constructor
	 */
	public TalkUi() {
		super();
		initialize();
		pack();
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		setSize(300,400);
		setLocation((dim.width-getWidth())/2,(dim.height-getHeight())/2);
		setVisible(true);
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setContentPane(getJContentPane());
		this.addWindowListener(new java.awt.event.WindowAdapter() { 
			public void windowClosing(java.awt.event.WindowEvent e) {    
//				TalkUi.this.leave(null);
				TalkUi.this.quit(null);
			}
		});
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new javax.swing.JPanel();
			java.awt.GridLayout layGridLayout7 = new java.awt.GridLayout();
			layGridLayout7.setRows(1);
			jContentPane.setLayout(layGridLayout7);
			jContentPane.add(getJPanel(), null);
		}
		return jContentPane;
	}
	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJPanel() {
		if(jPanel == null) {
			jPanel = new javax.swing.JPanel();
			jPanel.setLayout(new java.awt.BorderLayout());
			jPanel.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
			jPanel.add(getJPanel1(), java.awt.BorderLayout.SOUTH);
		}
		return jPanel;
	}
	/**
	 * This method initializes writerArea
	 * 
	 * @return jus.util.WriterArea
	 */
	private jus.util.WriterArea getWriterArea() {
		if(writerArea == null) {
			writerArea = new jus.util.WriterArea();
			writerArea.setEditable(false);
		}
		return writerArea;
	}
	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private javax.swing.JScrollPane getJScrollPane() {
		if(jScrollPane == null) {
			jScrollPane = new javax.swing.JScrollPane();
			jScrollPane.setViewportView(getWriterArea());
		}
		return jScrollPane;
	}
	/**
	 * This method initializes jPanel1
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJPanel1() {
		if(jPanel1 == null) {
			jPanel1 = new javax.swing.JPanel();
			java.awt.GridLayout layGridLayout6 = new java.awt.GridLayout();
			layGridLayout6.setRows(2);
			layGridLayout6.setColumns(4);
			jPanel1.setLayout(layGridLayout6);
			jPanel1.add(getEnterButton(), null);
			jPanel1.add(getLeaveButton(), null);
			jPanel1.add(getSayButton(), null);
			jPanel1.add(getWhoButton(), null);
			jPanel1.add(getClearButton(), null);
			jPanel1.add(getHelpButton(), null);
			jPanel1.add(getQuitButton(), null);
		}
		return jPanel1;
	}
	/**
	 * This method initializes sayButton
	 * 
	 * @return javax.swing.JButton
	 */
	private javax.swing.JButton getSayButton() {
		if(sayButton == null) {
			sayButton = new javax.swing.JButton();
			sayButton.setText("Say");
			sayButton.setEnabled(false);
			sayButton.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					say(e);
				}
			});
		}
		return sayButton;
	}
	/**
	 * This method initializes clearButton
	 * 
	 * @return javax.swing.JButton
	 */
	private javax.swing.JButton getClearButton() {
		if(clearButton == null) {
			clearButton = new javax.swing.JButton();
			clearButton.setText("Clear");
			clearButton.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					writerArea.clear();writerArea.out.flush();
				}
			});
		}
		return clearButton;
	}
	/**
	 * This method initializes whoButton
	 * 
	 * @return javax.swing.JButton
	 */
	private javax.swing.JButton getWhoButton() {
		if(whoButton == null) {
			whoButton = new javax.swing.JButton();
			whoButton.setText("Who");
			whoButton.setEnabled(false);
			whoButton.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					who(e);
				}
			});
		}
		return whoButton;
	}
	/**
	 * This method initializes helpButton
	 * 
	 * @return javax.swing.JButton
	 */
	private javax.swing.JButton getHelpButton() {
		if(helpButton == null) {
			helpButton = new javax.swing.JButton();
			helpButton.setText("Help");
			helpButton.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					JOptionPane.showMessageDialog(null,"no help available");
				}
			});
		}
		return helpButton;
	}
	/**
	 * This method initializes enterButton
	 * 
	 * @return javax.swing.JButton
	 */
	private javax.swing.JButton getEnterButton() {
		if(enterButton == null) {
			enterButton = new javax.swing.JButton();
			enterButton.setText("Enter");
			enterButton.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					enter(e);
				}
			});
		}
		return enterButton;
	}
	/**
	 * This method initializes leaveButton
	 * 
	 * @return javax.swing.JButton
	 */
	private javax.swing.JButton getLeaveButton() {
		if(leaveButton == null) {
			leaveButton = new javax.swing.JButton();
			leaveButton.setText("Leave");
			leaveButton.setEnabled(false);
			leaveButton.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					leave(e);
				}
			});
		}
		return leaveButton;
	}
	/**
	 * This method initializes quitButton
	 * 
	 * @return javax.swing.JButton
	 */
	private javax.swing.JButton getQuitButton() {
		if(quitButton == null) {
			quitButton = new javax.swing.JButton();
			quitButton.setText("Quit");
			quitButton.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					TalkUi.this.quit(e);
					TalkUi.this.setVisible(false);
					System.exit(1);
				}
			});
		}
		return quitButton;
	}
	/**
	 * This method initializes sayWriterArea
	 * 
	 * @return jus.util.WriterArea
	 */
	private jus.util.WriterArea getSayWriterArea() {
		if(sayWriterArea == null) {
			sayWriterArea = new jus.util.WriterArea();
			sayWriterArea.setPreferredSize(new java.awt.Dimension(200,100));
			sayWriterArea.setAutoscrolls(false);
		}
		return sayWriterArea;
	}
	/**
	 * This method initializes sayPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getSayPanel() {
		if(sayPanel == null) {
			sayPanel = new javax.swing.JPanel();
			sayPanel.add(getJScrollPane1(), null);
			sayPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED)), "What you want to say", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP, null, null));
		}
		return sayPanel;
	}
	/**
	 * This method initializes jScrollPane1
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private javax.swing.JScrollPane getJScrollPane1() {
		if(jScrollPane1 == null) {
			jScrollPane1 = new javax.swing.JScrollPane();
			jScrollPane1.setViewportView(getSayWriterArea());
		}
		return jScrollPane1;
	}
	/**
	 * This method initializes enterPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getEnterPanel() {
		if(enterPanel == null){
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(3);
			gridLayout.setColumns(2);
			jLabel2 = new JLabel();
			jLabel2.setText("Forum");
			jLabel1 = new JLabel();
			jLabel1.setText("Prénom");
			jLabel = new JLabel();
			jLabel.setText("Nom");
			enterPanel = new JPanel();
			enterPanel.setSize(new Dimension(250, 60));
			enterPanel.setPreferredSize(new Dimension(250, 60));
			enterPanel.setLayout(gridLayout);
			enterPanel.add(jLabel, null);
			enterPanel.add(getNomTextField(), null);
			enterPanel.add(jLabel1, null);
			enterPanel.add(getPrenomTextField(), null);
			enterPanel.add(jLabel2, null);
			enterPanel.add(getForumTextField(), null);
		}
		return enterPanel;
	}
	/**
	 * This method initializes nomTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getNomTextField() {
		if(nomTextField == null){
			nomTextField = new JTextField();
			nomTextField.setColumns(10);
			nomTextField.setText("Morat");
		}
		return nomTextField;
	}
	/**
	 * This method initializes prenomTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getPrenomTextField() {
		if(prenomTextField == null){
			prenomTextField = new JTextField();
			prenomTextField.setColumns(10);
			prenomTextField.setText("Philippe");
		}
		return prenomTextField;
	}
	/**
	 * This method initializes forumTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getForumTextField() {
		if(forumTextField == null){
			forumTextField = new JTextField();
			forumTextField.setColumns(20);
			forumTextField.setText("localhost/ski");
		}
		return forumTextField;
	}
}  //  @jve:visual-info  decl-index=0 visual-constraint="36,10"
